{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2024-04-25T01:34:53.814356400Z",
     "start_time": "2024-04-25T01:34:51.342536600Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.04790306091308594 0.018949031829833984\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import random\n",
    "import time\n",
    "\n",
    "a = list()\n",
    "for i in range(1000000):\n",
    "    a.append(random.random())\n",
    "\n",
    "t1 = time.time()\n",
    "sum1 = sum(a)\n",
    "t2 = time.time()\n",
    "\n",
    "b = np.array(a)\n",
    "t3 = time.time()\n",
    "sum2 = np.sum(b)\n",
    "t4 = time.time()\n",
    "\n",
    "print(t2 - t1, t4 - t3)"
   ]
  },
  {
   "cell_type": "markdown",
   "source": [
    "# numpy数组和python列表。\n",
    "# type是ndarray、list\n",
    "# 交互是array和tolist"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3]\n",
      "(3,)\n",
      "<class 'numpy.ndarray'>\n",
      "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n",
      "[[1, 2], [4, 5], [7, 8]]\n",
      "<class 'list'>\n",
      "[[1 2]\n",
      " [4 5]\n",
      " [7 8]]\n",
      "(3, 2)\n",
      "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n",
      "[ 9 12 88 14 25]\n",
      "1\n",
      "[9, 12, 88, 14, 25]\n"
     ]
    }
   ],
   "source": [
    "t1 = np.array([1, 2, 3])\n",
    "print(t1)\n",
    "print(t1.shape)\n",
    "print(type(t1))\n",
    "print(\"~\" * 50)\n",
    "\n",
    "list2 = [[1, 2], [4, 5], [7, 8]]\n",
    "print(list2)\n",
    "print(type(list2))\n",
    "t2 = np.array(list2)\n",
    "print(t2)\n",
    "print(t2.shape)\n",
    "print(\"~\" * 50)\n",
    "\n",
    "# tolist()方法将numpy数组转换为列表。数组没有逗号，列表有逗号\n",
    "a = np.array([9, 12, 88, 14, 25])\n",
    "print(a)\n",
    "print(a.ndim)\n",
    "list_a = a.tolist()\n",
    "print(list_a)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-04-25T02:34:02.181254100Z",
     "start_time": "2024-04-25T02:34:02.157967200Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "# numpy可以直接使用range"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "range(0, 10)\n",
      "<class 'range'>\n",
      "[0 1 2 3 4 5 6 7 8 9]\n",
      "[4 6 8]\n"
     ]
    }
   ],
   "source": [
    "print(range(10))\n",
    "print(type(range(10)))\n",
    "t2 = np.array(range(10))\n",
    "print(t2)\n",
    "t3 = np.arange(4, 10, 2)\n",
    "print(t3)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-04-25T02:55:58.605924700Z",
     "start_time": "2024-04-25T02:55:58.592365200Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "# numpy数组的属性"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2]\n",
      " [4 5]\n",
      " [7 8]]\n",
      "[1 2]\n",
      "[4 5]\n",
      "[7 8]\n",
      "\n",
      "2\n",
      "(3, 2)\n",
      "3\n",
      "2\n",
      "6\n"
     ]
    }
   ],
   "source": [
    "list2 = [[1, 2], [4, 5], [7, 8]]\n",
    "twoArray = np.array(list2)\n",
    "print(twoArray)\n",
    "print(twoArray[0])\n",
    "print(twoArray[1])\n",
    "print(twoArray[2])\n",
    "print()\n",
    "\n",
    "# 维度\n",
    "print(twoArray.ndim)\n",
    "\n",
    "# 行数*列数，元组\n",
    "print(twoArray.shape)\n",
    "print(twoArray.shape[0])  # 行数\n",
    "print(twoArray.shape[1])  # 列数\n",
    "\n",
    "# 元素个数\n",
    "print(twoArray.size)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-04-25T01:51:36.985812400Z",
     "start_time": "2024-04-25T01:51:36.964871500Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "# numpy数组的轴"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1848412748368\n",
      "1848412748368\n",
      "[[1 2]\n",
      " [3 4]\n",
      " [5 6]]\n",
      "--------------------------------------------------\n",
      "[[1 2]\n",
      " [3 4]\n",
      " [5 6]]\n",
      "1847923812560\n",
      "--------------------------------------------------\n",
      "[1 2 3 4 5 6]\n",
      "(6,)\n",
      "[1 2 3 4 5 6]\n",
      "(6,)\n",
      "--------------------------------------------------\n",
      "[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23]\n",
      "(24,)\n",
      "1\n",
      "\n",
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "(4, 6)\n",
      "2\n",
      "\n",
      "[[[ 0  1  2  3]\n",
      "  [ 4  5  6  7]\n",
      "  [ 8  9 10 11]]\n",
      "\n",
      " [[12 13 14 15]\n",
      "  [16 17 18 19]\n",
      "  [20 21 22 23]]]\n",
      "(2, 3, 4)\n",
      "3\n"
     ]
    }
   ],
   "source": [
    "four = np.array([[1, 2, 3], [4, 5, 6]])\n",
    "\n",
    "# 直接操作形状是在原地\n",
    "four1 = four\n",
    "four.shape = (3, 2)\n",
    "print(id(four))\n",
    "print(id(four1))\n",
    "print(four)\n",
    "print('-' * 50)\n",
    "\n",
    "# reshape方法则是返回一个新的数组\n",
    "four = four.reshape(3, 2)\n",
    "print(four)\n",
    "print(id(four))\n",
    "print('-' * 50)\n",
    "\n",
    "# 将多维变成一维数组，注意形状都是用元组表示的，所以要写成(6,)而不是(6)\n",
    "five = four.reshape((6,), order='C')\n",
    "# 展开成一维数组，默认情况下‘C’以行为主的顺序展开，‘F’（Fortran风格）意味着以列的顺序展开\n",
    "six = four.flatten(order='C')\n",
    "print(five)\n",
    "print(five.shape)\n",
    "print(six)\n",
    "print(six.shape)\n",
    "print('-' * 50)\n",
    "\n",
    "# 数组的一维二维三维\n",
    "t = np.arange(24)\n",
    "print(t)\n",
    "print(t.shape)\n",
    "print(t.ndim)\n",
    "print()\n",
    "# 转换成二维\n",
    "t1 = t.reshape((4, 6))\n",
    "print(t1)\n",
    "print(t1.shape)\n",
    "print(t1.ndim)\n",
    "print()\n",
    "# 转成三维\n",
    "# 0轴，1轴，2轴\n",
    "#最前面的括号是0轴，轴越小，越靠外，最小的轴是最外层\n",
    "t2 = t.reshape((2, 3, 4))\n",
    "print(t2)\n",
    "print(t2.shape)\n",
    "print(t2.ndim)\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-04-25T02:24:07.601963100Z",
     "start_time": "2024-04-25T02:24:07.590324300Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "# numpy数组元素的数据类型，dtype、itemsize、astype"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3 4 5]\n",
      "2\n",
      "int16\n",
      "8\n",
      "int64\n",
      "--------------------------------------------------\n",
      "0.7187721876503929\n",
      "0.48\n",
      "--------------------------------------------------\n",
      "[0.45303791 0.75680088 0.34912462 0.36006047 0.64363288 0.69020059\n",
      " 0.45192386 0.57091704 0.11724078 0.79572703]\n",
      "8\n",
      "float64\n",
      "[0.45 0.76 0.35 0.36 0.64 0.69 0.45 0.57 0.12 0.8 ]\n",
      "8\n",
      "float64\n"
     ]
    }
   ],
   "source": [
    "import random\n",
    "\n",
    "f = np.array([1, 2, 3, 4, 5], dtype=np.int16)  # dtype指定数据类型,int16表示16位整型，即数组中的每个元素占2个字节，默认32位\n",
    "print(f)\n",
    "print(f.itemsize)  # f中每个元素的字节数\n",
    "print(f.dtype)  # f中每个元素的数据类型\n",
    "\n",
    "# 调整数据类型\n",
    "f1 = f.astype(np.int64)\n",
    "print(f1.itemsize)\n",
    "print(f1.dtype)\n",
    "print('-' * 50)\n",
    "\n",
    "# 拓展随机生成小数\n",
    "print(random.random())  # 0-1之间的随机小数\n",
    "print(round(random.random(), 2))  # 保留两位小数\n",
    "print(\"-\" * 50)\n",
    "\n",
    "arr = np.array([random.random() for i in range(10)])  # 默认生成float64类型\n",
    "print(arr)\n",
    "print(arr.itemsize)\n",
    "print(arr.dtype)\n",
    "# 取小数点后两位\n",
    "arr1 = np.round(arr, 2)\n",
    "print(arr1)\n",
    "print(arr1.itemsize)  # 仍是8字节\n",
    "print(arr1.dtype)  # float64\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-04-25T02:48:48.353009300Z",
     "start_time": "2024-04-25T02:48:48.332685800Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "# numpy数组内部的运算，广播机制"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]\n",
      " [12 13 14 15]\n",
      " [16 17 18 19]\n",
      " [20 21 22 23]]\n",
      "--------------------\n",
      "[[ 2  3  4  5]\n",
      " [ 6  7  8  9]\n",
      " [10 11 12 13]\n",
      " [14 15 16 17]\n",
      " [18 19 20 21]\n",
      " [22 23 24 25]]\n",
      "--------------------\n",
      "[[ 0  2  4  6]\n",
      " [ 8 10 12 14]\n",
      " [16 18 20 22]\n",
      " [24 26 28 30]\n",
      " [32 34 36 38]\n",
      " [40 42 44 46]]\n",
      "--------------------\n",
      "[[ 0.   0.5  1.   1.5]\n",
      " [ 2.   2.5  3.   3.5]\n",
      " [ 4.   4.5  5.   5.5]\n",
      " [ 6.   6.5  7.   7.5]\n",
      " [ 8.   8.5  9.   9.5]\n",
      " [10.  10.5 11.  11.5]]\n"
     ]
    }
   ],
   "source": [
    "# 由于numpy的广播机机制在运算过程中，加减乘除的值被广播到所有的元素上面\n",
    "\n",
    "t1 = np.arange(24).reshape((6, 4))\n",
    "print(t1)\n",
    "print(\"-\" * 20)\n",
    "\n",
    "t2 = t1.tolist()\n",
    "print(t1 + 2)  # 偏置加法\n",
    "# print(t2+2) 不能对列表进行直接加整数操作\n",
    "print(\"-\" * 20)\n",
    "\n",
    "print(t1 * 2)\n",
    "print(\"-\" * 20)\n",
    "\n",
    "print(t1 / 2)\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-04-25T02:49:12.594997500Z",
     "start_time": "2024-04-25T02:49:12.560555700Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "# numpy数组之间的运算"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]\n",
      " [12 13 14 15]\n",
      " [16 17 18 19]\n",
      " [20 21 22 23]]\n",
      "[[100 101 102 103]\n",
      " [104 105 106 107]\n",
      " [108 109 110 111]\n",
      " [112 113 114 115]\n",
      " [116 117 118 119]\n",
      " [120 121 122 123]]\n",
      "[[100 102 104 106]\n",
      " [108 110 112 114]\n",
      " [116 118 120 122]\n",
      " [124 126 128 130]\n",
      " [132 134 136 138]\n",
      " [140 142 144 146]]\n",
      "[[   0  101  204  309]\n",
      " [ 416  525  636  749]\n",
      " [ 864  981 1100 1221]\n",
      " [1344 1469 1596 1725]\n",
      " [1856 1989 2124 2261]\n",
      " [2400 2541 2684 2829]]\n"
     ]
    }
   ],
   "source": [
    "#形状相同,才可以进行加减乘除\n",
    "t1 = np.arange(24).reshape((6, 4))\n",
    "t2 = np.arange(100, 124).reshape((6, 4))\n",
    "print(t1)\n",
    "print(t2)\n",
    "print(t1 + t2)\n",
    "print(t1 * t2)  #对应位置相乘，而不是矩阵乘法\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-04-25T02:49:15.757870600Z",
     "start_time": "2024-04-25T02:49:15.721968300Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "[[ 0  1  2  3  4  5  6  7]\n",
      " [ 8  9 10 11 12 13 14 15]\n",
      " [16 17 18 19 20 21 22 23]]\n"
     ]
    },
    {
     "ename": "ValueError",
     "evalue": "operands could not be broadcast together with shapes (4,6) (3,8) ",
     "output_type": "error",
     "traceback": [
      "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[1;31mValueError\u001B[0m                                Traceback (most recent call last)",
      "Cell \u001B[1;32mIn[70], line 6\u001B[0m\n\u001B[0;32m      4\u001B[0m \u001B[38;5;28mprint\u001B[39m(t1)\n\u001B[0;32m      5\u001B[0m \u001B[38;5;28mprint\u001B[39m(t2)\n\u001B[1;32m----> 6\u001B[0m \u001B[38;5;28mprint\u001B[39m(\u001B[43mt1\u001B[49m\u001B[38;5;241;43m-\u001B[39;49m\u001B[43mt2\u001B[49m)\n",
      "\u001B[1;31mValueError\u001B[0m: operands could not be broadcast together with shapes (4,6) (3,8) "
     ]
    }
   ],
   "source": [
    "#shape不同，不能进行运算\n",
    "t1 = np.arange(24).reshape((4, 6))\n",
    "t2 = np.arange(24).reshape((3, 8))\n",
    "print(t1)\n",
    "print(t2)\n",
    "print(t1 - t2)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-04-25T02:52:47.842092900Z",
     "start_time": "2024-04-25T02:52:47.799367100Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 数组之间运算，要么shape相同，要么行数列数有一个相同"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(6,)\n",
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "[0 1 2 3 4 5]\n",
      "[[ 0  0  0  0  0  0]\n",
      " [ 6  6  6  6  6  6]\n",
      " [12 12 12 12 12 12]\n",
      " [18 18 18 18 18 18]]\n",
      "--------------------------------------------------\n",
      "(4, 1)\n",
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "[[0]\n",
      " [1]\n",
      " [2]\n",
      " [3]]\n",
      "[[ 0  1  2  3  4  5]\n",
      " [ 5  6  7  8  9 10]\n",
      " [10 11 12 13 14 15]\n",
      " [15 16 17 18 19 20]]\n"
     ]
    }
   ],
   "source": [
    "#一维数组和二维数组进行运算时，一维的元素个数和列数相等\n",
    "t1 = np.arange(24).reshape((4, 6))\n",
    "t2 = np.arange(6)  # 一维的也可以运算\n",
    "#t2 = np.arange(6).reshape((1,6))\n",
    "print(t2.shape)\n",
    "print(t1)\n",
    "print(t2)\n",
    "print(t1 - t2)\n",
    "print(\"-\" * 50)\n",
    "\n",
    "t1 = np.arange(24).reshape((4, 6))\n",
    "t2 = np.arange(4).reshape((4, 1))\n",
    "print(t2.shape)\n",
    "print(t1)\n",
    "print(t2)\n",
    "print(t1 - t2)\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-04-25T03:02:55.356893100Z",
     "start_time": "2024-04-25T03:02:55.326232500Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 一、沿轴sum"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2 3]\n",
      " [4 5 6]]\n",
      "[5 7 9]\n",
      "--------------------\n",
      "[ 6 15]\n",
      "--------------------\n",
      "21\n",
      "--------------------\n",
      "[[[ 0  1  2]\n",
      "  [ 3  4  5]\n",
      "  [ 6  7  8]]\n",
      "\n",
      " [[ 9 10 11]\n",
      "  [12 13 14]\n",
      "  [15 16 17]]\n",
      "\n",
      " [[18 19 20]\n",
      "  [21 22 23]\n",
      "  [24 25 26]]]\n",
      "--------------------\n",
      "[[27 30 33]\n",
      " [36 39 42]\n",
      " [45 48 51]]\n",
      "--------------------\n",
      "[[ 9 12 15]\n",
      " [36 39 42]\n",
      " [63 66 69]]\n",
      "--------------------\n",
      "[[ 3 12 21]\n",
      " [30 39 48]\n",
      " [57 66 75]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "a = np.array([[1, 2, 3], [4, 5, 6]])\n",
    "\n",
    "print(a)\n",
    "print(np.sum(a, axis=0))  # [5 7 9]\n",
    "print(\"-\" * 20)\n",
    "\n",
    "print(np.sum(a, axis=1))  # [ 6 15]\n",
    "print(\"-\" * 20)\n",
    "\n",
    "print(np.sum(a))  # 21\n",
    "print(\"-\" * 20)\n",
    "\n",
    "a = np.arange(27).reshape((3, 3, 3))\n",
    "print(a)\n",
    "print(\"-\" * 20)\n",
    "\n",
    "b = np.sum(a, axis=0)\n",
    "print(b)\n",
    "print(\"-\" * 20)\n",
    "\n",
    "c = np.sum(a, axis=1)\n",
    "print(c)\n",
    "print(\"-\" * 20)\n",
    "\n",
    "d = np.sum(a, axis=2)\n",
    "print(d)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-04-25T04:04:45.473028500Z",
     "start_time": "2024-04-25T04:04:45.427891900Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 沿哪个轴，shape中那个轴就直接去掉，该轴上的数参与运算，即有关这个轴的元素不再体现\n",
    "# 剩下的括号重新012分配"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[[ 0  1  2  3]\n",
      "  [ 4  5  6  7]\n",
      "  [ 8  9 10 11]]\n",
      "\n",
      " [[12 13 14 15]\n",
      "  [16 17 18 19]\n",
      "  [20 21 22 23]]]\n",
      "--------------------\n",
      "[[12 14 16 18]\n",
      " [20 22 24 26]\n",
      " [28 30 32 34]]\n",
      "(3, 4)\n",
      "--------------------\n",
      "[[12 15 18 21]\n",
      " [48 51 54 57]]\n",
      "(2, 4)\n",
      "--------------------\n",
      "[[ 6 22 38]\n",
      " [54 70 86]]\n",
      "(2, 3)\n"
     ]
    }
   ],
   "source": [
    "a = np.arange(24).reshape((2, 3, 4))\n",
    "print(a)\n",
    "print(\"-\" * 20)\n",
    "\n",
    "b = np.sum(a, axis=0)\n",
    "print(b)\n",
    "print(b.shape)\n",
    "print(\"-\" * 20)\n",
    "\n",
    "c = np.sum(a, axis=1)\n",
    "print(c)\n",
    "print(c.shape)\n",
    "print(\"-\" * 20)\n",
    "\n",
    "d = np.sum(a, axis=2)\n",
    "print(d)\n",
    "print(d.shape)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-04-25T04:13:34.271090400Z",
     "start_time": "2024-04-25T04:13:34.251145300Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 二、索引和切片"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2 [0 1 2 3 4 5 6 7 8 9]\n",
      "[2 3 4 5 6 7 8 9]\n",
      "[2 4 6]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "a = np.arange(10)\n",
    "# 冒号分隔切片参数 start:stop:step 来进行切片操作print(a[2:7:2])# 从索引 2 开始到索引 7 停止，间隔为 2\n",
    "\n",
    "# 如果只放置一个参数，如 [2]，将返回与该索引相对应的单个元素\n",
    "print(a[2], a)\n",
    "\n",
    "# 如果为 [2:]，表示从该索引开始以后的所有项都将被提取\n",
    "print(a[2:])\n",
    "\n",
    "print(a[2:7:2])\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-04-25T04:32:12.508217500Z",
     "start_time": "2024-04-25T04:32:12.452176200Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 二维数组切片，list没有这种操作\n",
    "# 一层[]，提取相交区域。会改变提取出来的形状，一列会变成一行\n",
    "# [行要干什么，列要干什么]，两者相交，不填则默认 :"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 148,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "--------------------\n",
      "[ 6  7  8  9 10 11]\n",
      "--------------------\n",
      "[[ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "--------------------\n",
      "[[ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]]\n",
      "--------------------\n",
      "[ 1  7 13 19]\n",
      "--------------------\n",
      "[[ 1  2  3  4  5]\n",
      " [ 7  8  9 10 11]\n",
      " [13 14 15 16 17]\n",
      " [19 20 21 22 23]]\n",
      "--------------------\n",
      "15\n",
      "--------------------\n",
      "[[ 8  9]\n",
      " [14 15]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "t1 = np.arange(24).reshape(4, 6)\n",
    "print(t1)\n",
    "print('-' * 20)\n",
    "\n",
    "print(t1[1,])  # 取一行(一行代表是一条数据，索引也是从0开始的)\n",
    "# print(t1[1,:]) # 取一行\n",
    "print('-' * 20)\n",
    "\n",
    "print(t1[1:, ])  # 取连续的多行\n",
    "print('-' * 20)\n",
    "\n",
    "print(t1[1:3, :])  # 取连续的多行\n",
    "print('-' * 20)\n",
    "\n",
    "print(t1[:, 1])  # 当取一列时，这一列会变为一行作为一维数组\n",
    "print('-' * 20)\n",
    "\n",
    "print(t1[:, 1:])  # 连续的多列\n",
    "print('-' * 20)\n",
    "\n",
    "print(t1[2, 3])  # 取某一个值，即t1[2][3]\n",
    "print('-' * 20)\n",
    "\n",
    "print(t1[1:3, 2:4])  # 取1、2行，2、3列的相交区域"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-04-25T05:34:08.342962500Z",
     "start_time": "2024-04-25T05:34:08.317032500Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 两层[]，提取指定位置，组成一个新的数组，保持原有形状\n",
    "# [[哪几行],[哪几列]]，前后组合"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 147,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3  4  5]\n",
      " [18 19 20 21 22 23]\n",
      " [12 13 14 15 16 17]]\n",
      "--------------------\n",
      "[[ 0  2  3]\n",
      " [ 6  8  9]\n",
      " [12 14 15]\n",
      " [18 20 21]]\n",
      "--------------------\n",
      "[[ 1]\n",
      " [ 7]\n",
      " [13]\n",
      " [19]]\n",
      "--------------------\n",
      "[0 7 9]\n"
     ]
    }
   ],
   "source": [
    "print(t1[[0, 3, 2]])  # 取不连续的多行，0行，3行，2行\n",
    "#print(t1[[0,2,3],:])\n",
    "print('-' * 20)\n",
    "\n",
    "print(t1[:, [0, 2, 3]])  # 取不连续的多列\n",
    "print('-' * 20)\n",
    "\n",
    "print(t1[:, [1, ]])  # 取一列，且还保持列的形式\n",
    "print('-' * 20)\n",
    "\n",
    "print(t1[[0, 1, 1], [0, 1, 3]])  # 取多个不连续的值，[[行，行。。。],[列，列。。。]]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-04-25T05:32:23.564162200Z",
     "start_time": "2024-04-25T05:32:23.550200900Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 三、修改值"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 169,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "--------------------------------------------------\n",
      "[[10 10 10 10 10 10]\n",
      " [10 10 10 10 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 18 18 18 18 18]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "t = np.arange(24).reshape(4, 6)\n",
    "print(t)\n",
    "print('-' * 50)\n",
    "\n",
    "# 修改某一行的值\n",
    "# t[1,:]=0\n",
    "# print(t)\n",
    "# print('-'*50)\n",
    "\n",
    "# # 修改某一列的值\n",
    "# t[:,1]=0\n",
    "# print(t)\n",
    "# print('-'*50)\n",
    "\n",
    "# # 修改连续多行\n",
    "# t[1:3,:]=0\n",
    "# print(t)\n",
    "# print('-'*50)\n",
    "\n",
    "# # 修改连续多列\n",
    "# t[:,1:4]=0\n",
    "# print(t)\n",
    "# print('-'*50)\n",
    "\n",
    "# # 修改多行多列，取第二行到第四行，第三列到第五列\n",
    "# t[1:3,2:5]=0\n",
    "# print(t)\n",
    "# print('-'*50)\n",
    "\n",
    "# 修改多个不相邻的点\n",
    "# t[[0,1],[2,3]]=0\n",
    "# print(t)\n",
    "# print('-'*50)\n",
    "\n",
    "# 可以根据条件修改，比如讲小于10的值改掉\n",
    "# t[t<10]=0\n",
    "# print(t)\n",
    "# print(t<10) # 小于10的位置为True，大于10的位置为False\n",
    "# print('-'*50)\n",
    "\n",
    "# 使用逻辑判断\n",
    "# np.logical_and\t#&\n",
    "# np.logical_or\t#|\n",
    "# np.logical_not\t#~\n",
    "# t[(t>2)&(t<6)]=0\t# 逻辑与，and\n",
    "# t[(t<2)|(t>6)]=0\t# 逻辑或，or\n",
    "# t[~(t>6)]=0\t# 逻辑非\n",
    "# print(t)\n",
    "# print('-'*50)\n",
    "\n",
    "t = t.clip(10, 18)  # 限制数组的最小值和最大值，小于10的改为10，大于18的改为18\n",
    "print(t)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-04-25T08:21:40.416378200Z",
     "start_time": "2024-04-25T08:21:40.389369900Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 176,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[False False]\n",
      " [False False]\n",
      " [ True False]]\n",
      "[[80 88]\n",
      " [82 81]\n",
      " [75 81]]\n",
      "[[ 80  88]\n",
      " [ 82  81]\n",
      " [100  81]]\n"
     ]
    }
   ],
   "source": [
    "# 三目运算（ np.where(condition, x, y)，满足条件condition，输出x，不满足输出y。)）\n",
    "score = np.array([[80, 88], [82, 81], [75, 81]])\n",
    "result = np.where(score < 80, True, False)\n",
    "print(result)\n",
    "print(score)\n",
    "score[result] = 100  # 满足三目运算符条件的元素改为100\n",
    "print(score)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-04-25T08:25:51.070175800Z",
     "start_time": "2024-04-25T08:25:51.060021Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 4、数据的添加删除去重"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 202,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第一个数组：\n",
      "[[1 2 3]\n",
      " [4 5 6]]\n",
      " \n",
      "向数组添加元素：\n",
      "[ 1  2  3  4  5  6  7  8  9 10]\n",
      "\n",
      "原来的array还在：\n",
      "[[1 2 3]\n",
      " [4 5 6]]\n"
     ]
    }
   ],
   "source": [
    "# 1.numpy.append\n",
    "# numpy.append 函数在数组的末尾添加值。\n",
    "# 追加操作会返回一个新的数组，不会修改原数组。\n",
    "'''\n",
    "参 数 说 明 ：                                                                                                     arr： 原数组                                                                                                 values：要向arr添加的阵列\n",
    " axis：默认为 None，此时会展平数组，然后再添加元素，想加几个加几个。\n",
    "     如果指定了轴，则会在该轴方向上添加元素，此时有shape要求。\n",
    "'''\n",
    "import numpy as np\n",
    "\n",
    "a = np.array([[1, 2, 3], [4, 5, 6]])\n",
    "print('第一个数组：')\n",
    "print(a)\n",
    "\n",
    "print(' \\n向数组添加元素：')\n",
    "print(np.append(a, [7, 8, 9, 10], axis=None))  #ndarray会被展平\n",
    "\n",
    "print('\\n原来的array还在：')\n",
    "print(a)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-04-25T08:52:15.657922200Z",
     "start_time": "2024-04-25T08:52:15.633463Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 当要扩展array时，\n",
    "# 首先：看清楚array是几维的，比如array是2维，则要添加进去的阵列也是二维的\n",
    "# 然后：沿0轴方向添加，则这个二维阵列的shape必须是(任意，array.shape[1])\n",
    "#      沿1轴方向添加，则这个二维阵列的shape必须是(array.shape[0],任意)"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 198,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2 3]\n",
      " [4 5 6]]\n",
      "(2, 3) 2 3\n",
      "--------------------------------------------------\n",
      "2\n",
      "[[7 8 9]]\n",
      "(1, 3) 1 3\n",
      "--------------------------------------------------\n",
      "沿轴 0 添加元素：\n",
      "[[1 2 3]\n",
      " [4 5 6]\n",
      " [7 8 9]]\n",
      "--------------------------------------------------\n",
      "沿轴 1 添加元素：\n",
      "[[1 2 3 5 5]\n",
      " [4 5 6 7 8]]\n",
      "\n",
      "[[1 2 3]\n",
      " [4 5 6]]\n"
     ]
    }
   ],
   "source": [
    "print(a)\n",
    "print(a.shape, a.shape[0], a.shape[1])\n",
    "print('-' * 50)\n",
    "\n",
    "b = np.array([[7, 8, 9]])\n",
    "print(b.ndim)\n",
    "print(b)\n",
    "print(b.shape, b.shape[0], b.shape[1])\n",
    "print('-' * 50)\n",
    "\n",
    "print('沿轴 0 添加元素：')\n",
    "print(np.append(a, b, axis=0))\n",
    "print('-' * 50)\n",
    "\n",
    "print('沿轴 1 添加元素：')\n",
    "print(np.append(a, [[5, 5], [7, 8]], axis=1))\n",
    "print()\n",
    "print(a)\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-04-25T08:48:41.770268900Z",
     "start_time": "2024-04-25T08:48:41.741405200Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 221,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第一个数组：\n",
      "[[1 2]\n",
      " [3 4]\n",
      " [5 6]]\n",
      "(3, 2) 3 2\n",
      "\n",
      "未传递 Axis 参数。 在插入之前输入数组会被展开。\n",
      "[ 1 11 12  2  3  4  5  6]\n",
      "--------------------------------------------------\n",
      "[[ 1  2]\n",
      " [11 12]\n",
      " [ 3  4]\n",
      " [ 5  6]]\n",
      "\n",
      "[[ 1 11  2]\n",
      " [ 3 12  4]\n",
      " [ 5 13  6]]\n",
      "--------------------------------------------------\n",
      "沿轴0广播：\n",
      "[[ 1  2]\n",
      " [11 11]\n",
      " [ 3  4]\n",
      " [ 5  6]]\n",
      "\n",
      "沿轴1广播：\n",
      "[[ 1 -1  2]\n",
      " [ 3 -1  4]\n",
      " [ 5 -1  6]]\n"
     ]
    }
   ],
   "source": [
    "# 2. numpy.insert\n",
    "# 返回一个新数组\n",
    "# 如果未提供轴，则输入数组会被展开。\n",
    "# 和上面的append（拼接）不一样，这里是插入元素，所以不要求阵列维度和原array一样。但是shape要求和append一致\n",
    "\n",
    "a = np.array([[1, 2], [3, 4], [5, 6]])\n",
    "\n",
    "print('第一个数组：')\n",
    "print(a)\n",
    "print(a.shape, a.shape[0], a.shape[1])\n",
    "print()\n",
    "\n",
    "print('未传递 Axis 参数。 在插入之前输入数组会被展开。')\n",
    "print(np.insert(a, 1, [11, 12]))\n",
    "print('-' * 50)\n",
    "\n",
    "print(np.insert(a, 1, [11, 12], axis=0))\n",
    "# print(np.insert(a,1, [[11, 12]],axis=0)) # 效果一样\n",
    "# print('传递了 Axis 参数。 会广播值数组来配输入数组。')\n",
    "print()\n",
    "print(np.insert(a, 1, [11, 12, 13], axis=1))\n",
    "print('-' * 50)\n",
    "\n",
    "print('沿轴0广播：')\n",
    "print(np.insert(a, 1, 11, axis=0))\n",
    "# 相当于 print(np.insert(a, 1, [11, 11], axis=0))\n",
    "print()\n",
    "print('沿轴1广播：')\n",
    "print(np.insert(a, 1, -1, axis=1))\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-04-25T09:11:07.708426100Z",
     "start_time": "2024-04-25T09:11:07.696459100Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 228,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第一个数组：\n",
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]]\n",
      "\n",
      "未传递 Axis 参数。 在删除之前输入数组会被展开。\n",
      "[ 0  1  2  3  4  6  7  8  9 10 11]\n",
      "\n",
      "原array不变：\n",
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]]\n",
      "--------------------------------------------------\n",
      "删除第一行：\n",
      "[[ 0  1  2  3]\n",
      " [ 8  9 10 11]]\n",
      "\n",
      "删除第二列：\n",
      "[[ 0  1  3]\n",
      " [ 4  5  7]\n",
      " [ 8  9 11]]\n"
     ]
    }
   ],
   "source": [
    "# 3.numpy.delete\n",
    "# 返回从输入数组中删除指定子数组的新数组。\n",
    "# 与 insert() 函数的情况一样，\n",
    "# 如果未提供轴参数， 则输入数组将展开。\n",
    "\n",
    "import numpy as np\n",
    "\n",
    "a = np.arange(12).reshape(3, 4)\n",
    "\n",
    "print('第一个数组：')\n",
    "print(a)\n",
    "print('\\n未传递 Axis 参数。 在删除之前输入数组会被展开。')\n",
    "print(np.delete(a, 5))\n",
    "print('\\n原array不变：')\n",
    "print(a)\n",
    "print('-' * 50)\n",
    "\n",
    "print('删除第一行：')\n",
    "print(np.delete(a, 1, axis=0))\n",
    "print()\n",
    "print('删除第二列：')\n",
    "print(np.delete(a, 2, axis=1))\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-04-25T09:19:06.536929500Z",
     "start_time": "2024-04-25T09:19:06.519975800Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 240,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第一个数组：\n",
      "[5 2 6 2 7 5 6 9 8 2]\n",
      "--------------------------------------------------\n",
      "[2 5 6 7 8 9]\n",
      "[3 2 2 1 1 1]\n"
     ]
    }
   ],
   "source": [
    "# 4.numpy.unique\n",
    "# 用于去除数组中的重复元素\n",
    "# 依然返回新数组，该数组有序\n",
    "'''\n",
    "arr：输入数组，如果不是一维数组则会展开\n",
    "return_index：如果为true，返回新列表元素在旧列表中的位置（下标），并以列表形式储\n",
    "return_inverse：如果为true，返回旧列表元素在新列表中的位置（下标），并以列表形式储\n",
    "return_counts：如果为true，返回去重数组中的元素在原数组中的出现次数\n",
    "'''\n",
    "import numpy as np\n",
    "\n",
    "a = np.array([5, 2, 6, 2, 7, 5, 6, 9, 8, 2])\n",
    "\n",
    "print('第一个数组：')\n",
    "print(a)\n",
    "print('-' * 50)\n",
    "\n",
    "# 去除重复元素，输出的是有序序列\n",
    "# u = np.unique(a)\n",
    "# print (u)\n",
    "# print ('-'*50)\n",
    "\n",
    "# 可以返回新数组元素在原数组中的下标\n",
    "# u,indices = np.unique(a, return_index = True)\n",
    "# print(u)\n",
    "# print (indices)\n",
    "# print ('-'*50)\n",
    "\n",
    "# 返回旧列表元素在新列表中的位置（下标），并以列表形式储\n",
    "# u, indices = np.unique(a, return_inverse=True)\n",
    "# print(u)\n",
    "# print(indices)\n",
    "# print ('-'*50)\n",
    "\n",
    "# 返回去重元素的重复数量\n",
    "u,indices = np.unique(a,return_counts = True)\n",
    "print (u)\n",
    "print (indices)\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-04-25T09:57:52.101187800Z",
     "start_time": "2024-04-25T09:57:52.079247100Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 5、统计函数"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 246,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[80 88]\n",
      " [82 81]\n",
      " [75 81]]\n",
      "88\n",
      "[88 82 81]\n",
      "75\n",
      "[75 81]\n",
      "[0 0 0 1 2]\n",
      "[-2 -1  0  0  0]\n",
      "[1 2 4 1 5]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "score = np.array([[80,88],[82,81],[75,81]])\n",
    "print(score)\n",
    "# 1. 获取所有数据最大值\n",
    "result = np.max(score)\n",
    "print(result)\n",
    "# 2. 获取某一个轴上的数据最大值\n",
    "result = np.max(score,axis=1)\n",
    "print(result)\n",
    "# 3. 获取最小值\n",
    "result = np.min(score)\n",
    "print(result)\n",
    "# 4. 获取某一个轴上的数据最小值\n",
    "result = np.min(score,axis=0)\n",
    "print(result)\n",
    "# 5. 数据的比较\n",
    "# 第二个参数只是一个单独的值时，其实是用到了维度的广播机制；\n",
    "result = np.maximum( [-2, -1, 0, 1, 2], 0 )  # 第一个参数中的每一个数与第二个参数比较，返回大的\n",
    "print(result)\n",
    "result = np.minimum( [-2, -1, 0, 1, 2], 0 )  # 第一个参数中的每一个数与第二个参数比较，返回小的\n",
    "print(result)\n",
    "result = np.maximum( [-2, -1, 4, 1, 2], [1, 2, 3, 1, 5] ) # 两个参数的每一个元素比较，返回大的\n",
    "print(result)\n",
    "\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-04-25T10:07:59.390187800Z",
     "start_time": "2024-04-25T10:07:59.358270800Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 257,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[80 88]\n",
      " [82 81]\n",
      " [75 81]]\n",
      "81.16666666666667\n",
      "[79.         83.33333333]\n",
      "--------------------------------------------------\n",
      "[[1 2 3]\n",
      " [4 5 6]]\n",
      "[[1 2 3]\n",
      " [5 7 9]]\n",
      "[[ 1  3  6]\n",
      " [ 4  9 15]]\n",
      "--------------------------------------------------\n",
      "[2 1]\n",
      "[2.94392029 3.29983165]\n",
      "13\n"
     ]
    }
   ],
   "source": [
    "print(score)\n",
    "# 6. 求平均值\n",
    "result = np.mean(score) # 获取所有数据的平均值\n",
    "print(result)\n",
    "result = np.mean(score,axis=0) # 获取某一行或者某一列的平均值\n",
    "print(result)\n",
    "print('-' * 50)\n",
    "\n",
    "# 7. 沿轴求累加和\n",
    "arr\t= np.array([[1,2,3], [4,5,6]])\n",
    "print(arr)\n",
    "print(arr.cumsum(axis=0))\n",
    "'''\n",
    "[1, 2, 3]------>\t|1\t|2\t|3\t|\n",
    "[4, 5, 6]------>\t|5=1+4 |7=2+5 |9=3+6|\n",
    "'''\n",
    "print(arr.cumsum(axis=1))\n",
    "'''\n",
    "[1, 2, 3]------>\t|1\t|2+1\t|3+2+1\t|\n",
    "[4, 5, 6]------>\t|4\t|4+5\t|4+5+6\t|\n",
    "'''\n",
    "print('-' * 50)\n",
    "\n",
    "# 8. argmin求最小值索引，多个最小则返回第一个\n",
    "result = np.argmin(score,axis=0)\n",
    "print(result)\n",
    "\n",
    "# 9. 求每一列的标准差（这里是总体标准差）\n",
    "# 总体标准差的计算公式为：每个数减去均值，平方，求和，除以总数n，最后开根号\n",
    "# 样本标准差的计算公式为：每个数减去样本均值，平方，求和，除以样本数n-1，最后开根号\n",
    "# 反应出数据的波动稳定情况，越大表示越不稳定。越小表示越稳定。\n",
    "result = np.std(score,axis=0)\n",
    "print(result)\n",
    "\n",
    "# 10. 最大值和最小值的差\n",
    "result = np.ptp(score,axis=None)\n",
    "print(result)\n",
    "\n",
    "# 拓展：方差var, 协方差cov, 计算平均值 average, 计算中位数 median"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-04-25T10:24:12.321158600Z",
     "start_time": "2024-04-25T10:24:12.295275700Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
